package com.huarui.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.SetUtils;

import com.huarui.params.Params;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FuncUtil {
	/**
	 * 前台的参数Map转换
	 * @param map
	 * @return
	 */
	public static Map<String, Object> convertToParamMap(Map<String, String[]> map) {
		Set<Entry<String, String[]>> set = map.entrySet();
		Map<String, Object> pm = new HashMap<String, Object>();
		for(Entry<String, String[]> e : set) {
			if(e.getValue().length > 0) {
				pm.put(e.getKey(), e.getValue()[0]);
			}
		}
		return pm;
	}
	
	/**
	 * 前台的参数Map转换
	 * @param map
	 * @return
	 */
	public static Map<String, Object> converToParamMap(Map<String, Object> map) {
		Set<Entry<String, Object>> set = map.entrySet();
		Map<String, Object> pm = new HashMap<String, Object>();
		for(Entry<String, Object> e : set) {
			if(((Object[])e.getValue()).length > 0) {
				pm.put(e.getKey(), ((Object[])e.getValue())[0]);
			}
		}
		return pm;
	}
	

	@SuppressWarnings("unchecked")
	public static void removeNullValue(JSONObject jo) {
		Set<Entry> set = jo.entrySet();

		Iterator<Entry> it = set.iterator();

		while (it.hasNext()) {
			Entry<String, Object> ety = it.next();
			if (ety.getValue() instanceof JSONObject) {
				it.remove();
			}
		}
		
	
		
	}

	
	public static void removeNullValue(JSONArray ja) {
		for(int i=0; i<ja.size(); i++) {
			JSONObject jo = ja.getJSONObject(i);
			removeNullValue(jo);
		}
	}
	
//	public static void putMesMap(Map<String, Object> mesMap, String m, Integer ordersId) {
//		if("Y".equals(m)) {
//			mesMap.put(Params.PUR_ORDER_ID_PARAM, ordersId);
//			mesMap.put(Params.PUR_ORDER_STATUS_PARAM, PurOrSaleOrder.STATUS_TMP);
//			mesMap.put(Params.PUR_ALERT_MES, "m1");
//			mesMap.put(Params.PUR_LOADED_MESS_KEYS, "f1:f2:f3");
//		} else if("C".equals(m)) {
//			mesMap.put(Params.PUR_ORDER_ID_PARAM, ordersId);
//			mesMap.put(Params.PUR_ORDER_STATUS_PARAM, PurOrSaleOrder.STATUS_TMP + "-");
//			mesMap.put(Params.PUR_ALERT_MES, "m2");
//			mesMap.put(Params.PUR_LOADED_MESS_KEYS, "f1:f2:f3");
//		} else if("S".equals(m)) {
//			mesMap.put(Params.PUR_ORDER_ID_PARAM, ordersId);
//			mesMap.put(Params.PUR_ORDER_STATUS_PARAM, PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT);
//			mesMap.put(Params.PUR_ALERT_MES, "m3");
//			mesMap.put(Params.PUR_LOADED_MESS_KEYS, "f1:f2:f3");
//		} else {
////			mesMap.put(Params.PUR_ORDER_ID_PARAM, ordersId);
//			mesMap.put(Params.PUR_ORDER_STATUS_PARAM, PurOrSaleOrder.STATUS_INITIAL);
//			mesMap.put(Params.PUR_ALERT_MES, "m4");
//			mesMap.put(Params.PUR_LOADED_MESS_KEYS, "f3");
//		}
//	}
	
	public static BigDecimal addPurAmount(List<PurOrSaleOrderDetail> list) {
		BigDecimal amount = new BigDecimal(0.0);
		for (PurOrSaleOrderDetail p : list) {
			p.setQuantity(p.getOrderQuantity());
			amount = amount.add(p.getPrice().multiply(
					new BigDecimal(p.getQuantity())));
		}
		return amount;
	}
	
	public static BigDecimal addSaleAmount(List<PurOrSaleOrderDetail> list) {
		BigDecimal amount = new BigDecimal(0.0);
		for (PurOrSaleOrderDetail p : list) {
			amount = amount.add(p.getPrice().multiply(
					new BigDecimal(p.getQuantity())));
		}
		return amount;
	}
	
	/**
	 * 转换IP地址为长类型
	 * @param ip
	 * @return
	 */
	public static final long ip2Long(String ip) {
		long num[] = new long[4];
		int index1 = ip.indexOf(".");
		int index2 = ip.indexOf(".", index1 + 1);
		int index3 = ip.indexOf(".", index2 + 1);
		
		num[0] = Long.parseLong(ip.substring(0, index1));
		num[1] = Long.parseLong(ip.substring(index1 + 1, index2));
		num[2] = Long.parseLong(ip.substring(index2 + 1, index3));
		num[3] = Long.parseLong(ip.substring(index3 + 1));
		
		return (num[0] << 24) + (num[1] << 16) + (num[2] << 8) + num[3];
	}
}
